package com.lxf.cn.app;

import com.alibaba.fastjson.JSONObject;
import com.lxf.cn.config.BaseController;
import com.lxf.cn.model.User;
import com.lxf.cn.redis.RedisUitls;
import com.lxf.cn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liaoxuefeng on 2018/12/6.
 */

@Controller
@RequestMapping("/api")
public class AppIndexController extends BaseController {

    @Autowired
    UserService userService ;

    @Autowired
    RedisUitls redisUitls ;

    @RequestMapping("/index")
    public @ResponseBody Object getOrder(){
        Map map = new HashMap();
        map.put("exceptionMsg","1");
        map.put("name","订单");
        map.put("message","登录失败,用户不存在");
        throw new RuntimeException("用户不存在，请重新登录！！！！！！！！！！！");
    }





    @RequestMapping("/index1")
    public @ResponseBody Object getOrder1(){
        return "密码修改成" ;
      //  return false ;
     //  throw new RuntimeException("用户不存在，请重新登录");
    }


    /**
     *
     * @param params
     * @param session
     * @return
     */
    @RequestMapping("login")
    public @ResponseBody Object login(@RequestBody JSONObject params , HttpSession session){

        String name = params.getString("name");
        Assert.notNull(name , "用户名不能为空");
        String pwd = params.getString("pwd");
        Assert.notNull(pwd , "密码不能为空");

        User user = userService.findUser(params);
        if(user == null){
            throw new RuntimeException("用户不存在请重新输入");
        }
        String token =  Token.getToken(user) ;
        //设置token 时间为一天分钟
        redisUitls.setValueAndLifeCycle("appToken"+user.getUserId().toString(), 60*60*24, token);

        Map map = new HashMap();
        map.put("id" , user.getUserId() );
        map.put("name" , name );
        map.put("pwd", pwd);
        map.put("" ,token );
        return map ;

    }

    /**
     * 返回登录成功
     * @return
     */
    @RequestMapping("/main")
    public String index(){
        return "main.html";
    }




}
